const { app,BrowserWindow,Menu,ipcMain } = require("electron")
const isDev = require("electron-is-dev")
const path =require("path")
const menuTemplate = require("./src/menuTemplate")
const { appWindow } = require("./src/utils/appWindow")
let mainWindow,settingWindow;

app.on('ready',()=>{
  mainWindow = new appWindow({
    width:1024,
    height:680,
  })
  mainWindow.once('ready-to-show', () => {
    mainWindow.show()
  })
  mainWindow.on("closed",()=>{
    mainWindow = null;
  })

  const urlLocation = isDev ? 'http://localhost:3000':`file://${path.join(__dirname,'./build/index.html')}`
  mainWindow.loadURL(urlLocation);

  const settingPath = path.join(__dirname,"settings/settings.html")

  //打开设置按钮
  ipcMain.on("open-settings-window",function(){
    settingWindow = new appWindow({
       width:500,
       height:400,
       parent: mainWindow
    })
    // settingWindow.removeMenu();
    settingWindow.once('ready-to-show', () => {
      settingWindow.show()
    })
    settingWindow.on("closed",()=>{
      settingWindow = null;
    })
    // settingWindow.loadFile(settingPath)
    settingWindow.loadURL(`file://${settingPath}`)
  })

  const menu = Menu.buildFromTemplate(menuTemplate)
  Menu.setApplicationMenu(menu)
})
